

const deepMerge = (minor, main) => {
  for (var key in minor) {
    if (main[key] === undefined) {
      main[key] = minor[key];
      continue;
    }
    if (typeof minor[key] == "object" && minor[key].constructor == Object) {
      deepMerge(minor[key], main[key]);
    }
  }
}

// 原生Page属性
const properties = ['data', 'onLoad', 'onReady', 'onShow', 'onHide', 'onUnload',
  'onPullDownRefresh', 'onReachBottom', 'onShareAppMessage', 'onPageScroll', 'onTabItemTap'
];
const mixDecorator = (mixin, options) => {
  if (!mixin) {
    return options;
  }

  let mixObject = {};
  for (let [key, value] of Object.entries(mixin)) {
    if (key === 'data') {
      deepMerge(value,options.data);
      options.data = options.data;      
    } else if (properties.includes(key)) {
      let native = options[key];
      mixObject[key] = function(...args) {
        value.call(this, ...args);
        return native && native.call(this, ...args);
      }
    } else {
      mixObject = Object.assign({}, mixin, options);
    }
  }
  return mixObject;
}

export default mixDecorator;